<?php
namespace cms\modules\v1\models\versions;

use cms\modules\v1\models\Common;
use common\models\Functions;
use common\models\Status;
use yii\base\Exception;
use Yii;
/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class Versions extends \common\models\versions\Versions
{

    public function rules()
    {
        return [
            [['version_code','version_name','version_info','download_url'],'required','message' => "{attribute}不能为空", 'on' => ['addEdit']],//两个值必须有
            [['version_name','version_code','download_url','version_info'],'filter','filter' => 'trim', 'on' => ['addEdit']],
        ];
    }

    public function attributeLabels()
    {
        return [
            'version_code' => '版本号',
            'version_name' => '版本名称',
            'version_info' => '更新信息',
            'download_url' => '下载地址',
        ];

    }

    /**
     * 设置场景
     * @return array
     */
    public function scenarios()
    {
        $scenarios = [
            'addEdit' => [
                'version_code',
                'version_name',
                'version_info',
                'download_url',
            ],
        ];
        return array_merge(parent::scenarios(), $scenarios);
    }


    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getListData($page = 1, $size = 20) {
        try {

            //搜索条件
            $searchWhere  = $this->getSearch();//自定义搜索条件

            //得到文章的总数
            $count = self::getCountByAndWhere($searchWhere['where'], $searchWhere['andWhere']);
            $data = static::setPageinationAPI($page, $size, $count);

            //设置分页
            $offset = ($data['page'] - 1) * $size;

            //排序 order
            $orderSql = 'id ASC';

            $list = static::getByAndWhere($searchWhere['where'],$searchWhere['andWhere'], '*', $orderSql, $offset, $size);

            $data['data'] = $this->handelList($list);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     * @return string
     */
    public function getSearch () {
        $where = $andWhere = [];
        return [
            'where' => $where,
            'andWhere' => $andWhere
        ];
    }

    /**
     * 处理初始化数据
     * @param $list
     * @return mixed
     */
    public function handelList($list) {
        if (empty($list)) {
            return null;
        }

        foreach ($list as $key => $value) {
        }
        return $list;
    }

    /**
     * 获取修改信息
     * @param int $id
     * @return array
     */
    public function getInfo($id = 0) {
        try {

            if (empty($id)) {
                throw new Exception('未知信息');
            }

            $info = static::getOneInfoById($id);
            if (empty($info)) {
                throw new Exception('未知信息');
            }
            $info['version_code'] = (int) $info['version_code'];
            return Functions::formatRestAPI(0, '', $info);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 添加修改
     * @return array
     */
    public function addEdit() {
        $trans = Yii::$app->db->beginTransaction();
        try {
            if (!$this->validate()) {
                throw new Exception($this->getErrorSummary(true)[0]);
            }


            $post = Yii::$app->request->post();
            $id = isset($post['id']) ? $post['id'] : 0;
            $data = [];
            $data = array_merge($data, $this->getSaveData($this->scenario, $post));
            if (!empty($id)) {
                //更新操作
                $data['update_time'] = date('Y-m-d H:i:s');
                $res = static::updateDataWithLog($data, ['id' => $id]);
                if ($res === false) {
                    throw new Exception('更新失败');
                }
            } else {
                //添加操作
                $data['update_time'] = $data['create_time'] = date('Y-m-d H:i:s');

                $res = static::insertDataWithLog($data);
                if ($res === false) {
                    throw new Exception('添加失败');
                }

            }
            $trans->commit();
            return Functions::formatRestAPI(0, '操作成功');
        } catch (Exception $e) {
            $trans->rollBack();
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

}
